<?php

iconv_set_encoding("internal_encoding", "UTF-8");
class Model {

	private $conn;

	//大田ID，前提条件，当前有且仅有一个大田，否则setLandId无法正确updata，getLandId也无法正确获取(即land和block之间无明确关系)。
	private $landId;

	private $config;

	function __construct($config)	
	{
		$this->config = $config;
	}

	public function getConfig()
	{
		if(!isset($this->config)) {
			$this->config = require('Config.php');
		}
		return  $this->config;
	}

	public function getDb()
	{
		if(!isset($this->conn)) {
			try {
				 //$this->conn = new PDO($this->getConfig()['db']['dsn'], $this->getConfig()['db']['user'], $this->getConfig()['db']['password']);   /*原写法php 5.5.14 php 5.3.5不支持  */
			    $db = $this->getConfig();
				$this->conn = new PDO($db['db']['dsn'], $db['db']['user'], $db['db']['password']);     //2015.08.04				
			} catch (PDOException $e) {
				Log::logger('Error!! Could not connect: ' . $e->getMessage());
				die('Could not connect: ' . $e->getMessage());
			}
		}
		return $this->conn;
	}

	public function getLandInfo()
	{
		$conn = $this->getDb();
		$sql = 'select ID, LandAddr from basicinfor where Landid <= 0 limit 1';
		try {
			$res = $conn->query($sql);
		} catch (PDOException $e) {
			Log::logger('Error!! query landinfo faild');
			die('query landinfo faild');
		}
		$data = $res->fetch();
		$landInfo = array();
		if($data) {
			$landInfo_ID = $data['ID'];
			$landInfo['class'] = "1";			
			$data['LandAddr'] = iconv("GBK//IGNORE", "UTF-8", $data['LandAddr']);
			$landInfo['addr'] = $data['LandAddr'];
		}
		var_dump($landInfo);
		return $landInfo ? array($landInfo_ID,$landInfo) : array(0,NUll);
	}

	/**
	* 获得大田ID，当大田多于一个时，需要将大田和地块联系起来，此时不应通过该方法获得大田ID
	*/
	public function getLandId()
	{
		if(isset($this->landId)) {
			return $this->landId;
		}

		$conn = $this->getDb();
		$sql = 'select Landid from basicinfor where Landid > 0 limit 1';
		try {
			$res = $conn->query($sql);
		} catch (PDOException $e) {
			Log::logger('Error!! query landid faild');
			die('query landid faild');
		}
		$data = $res->fetch();
		if($data) {
			$this->landId = $data['Landid'];
		}
		return $this->landId ? $this->landId : null;
	}

	public function setLandId($landId, $ID = null)
	{
		// if(!isset($landId) || !isset($ID)) {
		if(!isset($landId)) {
			return false;
		}

		$conn = $this->getDb();
		$sql = 'update basicinfor set Landid = ' . $landId . ' where Landid <= 0 and ID = ' . $ID;
		try {
			$conn->exec($sql);
			$this->landId = $landId;
		} catch (PDOException $e) {
			Log::logger('Error!! set landid faild');
			die('set landid faild');
		}
		return true;
	}

	public function getMultiBlockInfo()
	{
		$conn = $this->getDb();
		$sql = 'select ID, Cropid, Cropname, Unit, Count, produce_expert, produce from irrdata where SubLandid <= 0';
		try {
			$res = $conn->query($sql);
		} catch (PDOException $e) {
			Log::logger('Error!! query MultiBlockInfo faild');
			die('query MultiBlockInfo faild');
		}
		$data = $res->fetchAll();
		$multiBlockInfo = array();
		foreach ($data as $value) {
			$BlockInfo['ID'] = $value['ID'];
			$BlockInfo['land_id'] = $this->getLandId();
			$BlockInfo['crop_id'] = $value['Cropid'];
			$BlockInfo['crop_name'] =iconv("GBK//IGNORE", "UTF-8",  $value['Cropname']);
			$BlockInfo['unit'] = iconv("GBK//IGNORE", "UTF-8",  $value['Unit']);
			$BlockInfo['count'] = $value['Count'];
			$BlockInfo['produce_expert'] = $value['produce_expert'];
			$BlockInfo['produce'] = $value['produce'];
			array_push($multiBlockInfo, $BlockInfo);
		}
		return $multiBlockInfo ? $multiBlockInfo : null;
	}

	public function setSubLandID($subLandId, $ID)
	{
		if(!isset($subLandId) || !isset($ID)) {
			return false;
		}

		$conn = $this->getDb();
		$sql = 'update irrdata set SubLandid = ' . $subLandId . ' where SubLandid <= 0 and ID = ' . $ID;
		try {
			$conn->exec($sql);
		} catch (PDOException $e) {
			Log::logger('Error!! set SubLandid faild');
			die('set SubLandid faild');
		}
		return true;
	}

	public function getMultiBlockEnvInfo()
	{
		$conn = $this->getDb();
		$sql = 'select SubLandid, IrrSoilTemp, IrrSoilRH from irrdata where SubLandid >0';
		try {
			$res = $conn->query($sql);
		} catch (PDOException $e) {
			Log::logger('Error!! query envInfo faild');
			die('query envInfo faild');
		}
		$dataEnvs = $res->fetchAll();
		$sql = 'select OutTemp, OutRH from outweather limit 1';
		try {
			$res = $conn->query($sql);
		} catch (PDOException $e) {
			Log::logger('Error!! query envInfo faild');
			die('query envInfo faild');
		}
		$dataWeather = $res->fetch();
		$multiBlockEnvInfo["rows"] = array();
		if($dataEnvs && $dataWeather) {
			foreach ($dataEnvs as $dataEnv) {
				$BlockEnvInfo['land_id'] = $this->getLandId();
				$BlockEnvInfo['sub_land_id'] = $dataEnv['SubLandid'];
				$BlockEnvInfo['soil_temp'] = iconv("GBK//IGNORE", "UTF-8",  $dataEnv['IrrSoilTemp']);
				$BlockEnvInfo['soil_humidity'] = iconv("GBK//IGNORE", "UTF-8",  $dataEnv['IrrSoilRH']);
				$BlockEnvInfo['air_temp'] = iconv("GBK//IGNORE", "UTF-8",  $dataWeather['OutTemp']);
				$BlockEnvInfo['air_humidity'] = iconv("GBK//IGNORE", "UTF-8",  $dataWeather['OutRH']);
				array_push($multiBlockEnvInfo["rows"], $BlockEnvInfo);
			}
		}
		return $multiBlockEnvInfo ? $multiBlockEnvInfo : null;
	}
}
?>